package cn.antscrpt.antLib.Effects 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BitmapFilter;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.filters.ShaderFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import org.libspark.betweenas3.BetweenAS3;
	
	import gs.TweenLite;
	import gs.easing.*

	
	/**
	 * ...
	 * @author microthink AntScript
	 */
	public class ParticleMoving extends Sprite
	{
		
		private var _displayTarget:DisplayObject;
		private var _targetBitmapData:BitmapData;
		private var _bgWidth:int;
		private var _bgHeight:int;
		private var _autoPlay:Boolean;
		private var _alphaEnabled:Boolean;
		
		
		
		private var _displayBitmap:Bitmap;
		private var _canvas:BitmapData;
		
		private var _particles:Vector.<P>;
		
		public function ParticleMoving(displayTarget:DisplayObject, bgWidth:int, bgHeight:int, autoPlay:Boolean = true, alphaEnabled:Boolean = false) 
		{			
			_displayTarget = displayTarget;
			_bgWidth = bgWidth;
			_bgHeight = bgHeight;	
			_autoPlay = autoPlay;
			_alphaEnabled = alphaEnabled;
			
			init();
		}
		
		private function init():void
		{
			initDisplayTarget();
			initCanvas();
			
			play();			
		}
		
		private function initDisplayTarget():void
		{
			_targetBitmapData = new BitmapData(_bgWidth,_bgHeight, true, 0x00000000);
			_targetBitmapData.draw(_displayTarget,new Matrix(1,0,0,1,0,0));
			
			
			_particles = new Vector.<P>();
			var loopX:int = _targetBitmapData.width;
			var loopY:int = _targetBitmapData.height;
			var xx:int;
			var yy:int;
			if (!_alphaEnabled) {
				for (yy = 0; yy < loopY; yy++) 
				{
					for (xx = 0; xx < loopX; xx++) 
					{
						if(((_targetBitmapData.getPixel32(xx,yy)>>24)&0xff)!=0){
							var particle:P = new P(xx, yy, _targetBitmapData.getPixel(xx, yy));
							particle.x = Math.random() * _bgWidth;
							particle.y = Math.random() * _bgHeight;
							_particles.push(particle);
							//BetweenAS3.tween(particle, {x: particle.targetX,y:particle.targetY},null,.5).play();
							//TweenLite.to(particle, 1, {x:particle.targetX, y:particle.targetY, ease:Cubic.easeIn});
							
						}
					}
				}
			}
			else {
				for (yy = 0; yy < loopY; yy++) 
				{
					for (xx = 0; xx < loopX; xx++) 
					{
						if((_targetBitmapData.getPixel32(xx,yy)>>24&0xff)!=0){//trace((0xff-(_targetBitmapData.getPixel32(xx,yy)>>24)&0xff)/0xff)
							
							
							
						}
					}
				}
			}
			
			
			_displayTarget = null;
			_targetBitmapData.dispose();
			_targetBitmapData = null;
			
			
			
		}
		
		private function initCanvas():void
		{
			_displayBitmap = new Bitmap();
			addChild(_displayBitmap);
			
			_canvas = new BitmapData(_bgWidth, _bgHeight);
			_displayBitmap.bitmapData = _canvas;
			
		}
		
		
		
		public function play():void {
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		public function stop():void {
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onEnterFrame(e:Event):void 
		{
			
			_canvas.lock();
			_canvas.fillRect(_canvas.rect, 0x00000000);
			var loop:int = _particles.length;
			for (var i:int = 0; i < loop; i++) 
			{
				if (Math.abs(_particles[i].targetX - _particles[i].x) < 10 && Math.abs(_particles[i].targetY - _particles[i].y) < 10) {
					_particles[i].x =Math.random() * _bgWidth;
					_particles[i].y = Math.random() * _bgHeight;
					//var d:Number = Math.sqrt((_particles[i].targetX - _particles[i].x) * (_particles[i].targetX - _particles[i].x) + (_particles[i].targetY - _particles[i].y) * (_particles[i].targetY - _particles[i].y))/Math.sqrt((_bgWidth/2)*(_bgWidth/2)+(_bgHeight/2)*(_bgHeight/2));
					//TweenLite.to(_particles[i], 0.5*d, {x:_particles[i].targetX, y:_particles[i].targetY, ease:Cubic.easeIn});
					//BetweenAS3.tween(_particles[i], {x: _particles[i].targetX,y:_particles[i].targetY},null,0.5*d).play();
				}
				else {
					_particles[i].x = _particles[i].x +(_particles[i].targetX - _particles[i].x)*0.1*(Math.random()+0.5);
					_particles[i].y = _particles[i].y + (_particles[i].targetY - _particles[i].y)*0.1*(Math.random()+0.5);
				}
				
				_alphaEnabled?_canvas.setPixel32(_particles[i].x, _particles[i].y, _particles[i].color&0xffffffff):_canvas.setPixel32(_particles[i].x, _particles[i].y, _particles[i].color+0xff000000);
			}
			
			_canvas.unlock();
			
		}
		
		
		
		
		
		
		
		
	}
	
}
class P {
	public var x:int;
	public var y:int;
	public var targetX:int;
	public var targetY:int;
	public var color:int;
	
	public function P(tX:int, tY:int,c:int) {
		targetX = tX;
		targetY = tY;
		color = c;
	}
}